<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"  
               ref="crud" 
               id="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuRight">
        <table-right :crud='$refs.crud' :data='data' :refreshChange='refreshChange'>
          <el-button size="mini" type="text" icon="el-icon-plus"  @click="addFn"  >新增</el-button>
        </table-right>
      </template>
      <template slot-scope="{row}" slot="menu" width="360px">
        <el-button   size="small" type="text"  @click="viewFn(row)"  >查看</el-button> 
        <el-button   size="small" type="text"  @click="editFn(row)"  >编辑</el-button> 
        <el-button   size="small" type="text"  @click="delFun(row)"  >删除</el-button>
      </template>   
    </avue-crud>
    <el-dialog :title="title"
               v-dialogdrag
               :destroy-on-close="true"
               show-close
               :visible.sync="showDialog"
               width="60%"
               append-to-body='true'
               top="3%"
               :close-on-click-modal='false'>
      <package-config-form @reload="reload" @closeAfter="closeAfter" :type="openType"  :row="clickRow"  @close="showDialog=false" v-if="showDialog" />
    </el-dialog>
  </basic-container>
</template>

<script>
  import * as api from "@/api/upgrade/packageFrontConfig";
  import {mapGetters} from "vuex"; 
  import {init} from '@/mixins/init'; 
  import PackageConfigForm from "@/views/upgrade/packageFrontConfigForm";
  export default {
    components: {PackageConfigForm},
    mixins: [init],
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 100,
          currentPage: 1,
          total: 0
        },
        title:'升级配置',
        showDialog: false,
        openType:'add',
        clickRow:null,
        selectionList: [],
        option: { 
          dialogWidth: '1000',
          labelWidth: 120,
          searchGutter: 0,
          menuWidth: 120,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          selection: true,
          menu:true,
          column: [            
            {
              label: "模块",
              prop: "sysModule",
              search: true, 
            },     
            {
              label: "目标地址",
              prop: "sysRunDir" ,  
            },         
            {
              label: "启动命令",
              prop: "sysStartShell" ,  
            },  
            {
              label: "创建时间",
              width: 150,
              prop: "createTime"               
            }, 
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.post_add, false),
          viewBtn: this.vaildData(this.permission.post_view, false),
          delBtn: this.vaildData(this.permission.post_delete, false),
          editBtn: this.vaildData(this.permission.post_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      viewFn(inRow){
        this.openType = 'view';
        this.clickRow = inRow;
        this.showDialog = true;          
      },
      addFn(){
        this.openType = 'add';
        this.clickRow = null;
        this.showDialog = true;          
      },
      editFn(inRow){
        this.openType = 'edit';
        this.clickRow = inRow;
        this.showDialog = true;  
      },
      delFun(row){ 
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return api.remove(row.id);
          })
          .catch(()=>{}) // 添加错误捕获
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },      
      reload () {
        this.onLoad(this.page)
        this.showDialog = false
      },
      closeAfter(){
        this.onLoad(this.page);
      },        
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;          
        api.getPage(page.currentPage, page.pageSize, this.query).then(res => {
          const data = res.data.data; 
          if(data){
            this.page.total = data.total;
            this.data = data.records;
          } else {
            this.page.total = 0;
            this.data = [];
          }         
          this.loading = false; 
        });
      }
    }
  };
</script>

<style>
</style>
